package org.yi.web.post.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.yi.core.annotation.TableBind;
import org.yi.core.common.Constants;
import org.yi.core.enums.PostStateEnum;
import org.yi.core.model.Pagination;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

@TableBind(name="f_post")
public class PostEntity extends Model<PostEntity> {

	private static final long serialVersionUID = -2051861834676654179L;

	public static PostEntity dao = new PostEntity();

	public Page<PostEntity> getPager(Pagination pager, Controller c) {
		String select = "select * ";
		StringBuffer where = new StringBuffer("from f_post where 1=1 and state != '"+PostStateEnum.DELETE.getCode()+"'");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(c.getPara("category"))) {
			where.append("and category = ?");
			params.add(c.getPara("category"));
		}
		if(StringUtils.isNotBlank(c.getPara("state"))){
			where.append("and state = ?");
			params.add(c.getPara("state"));
		}
		if(StringUtils.isNotBlank(c.getPara("title"))){
			where.append("and title like ?");
			params.add("%"+c.getPara("title")+"%");
		}
		if(StringUtils.isNotBlank(c.getPara("createTimeMin"))){
			where.append("and create_time >= ?");
			params.add(c.getPara("createTimeMin"));
		}
		if(StringUtils.isNotBlank(c.getPara("createTimeMax"))){
			where.append("and create_time < ?");
			params.add(c.getPara("createTimeMax"));
		}
		return dao.paginate(pager.getPn(), pager.getSize(), select, where.toString(), params.toArray());
	}
	
	public Page<PostEntity> getRecyclePager(Pagination pager, Controller c) {
		String select = "select * ";
		StringBuffer where = new StringBuffer("from f_post where 1=1 and state = '"+PostStateEnum.DELETE.getCode()+"'");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(c.getPara("category"))) {
			where.append("and category = ?");
			params.add(c.getPara("category"));
		}
		if(StringUtils.isNotBlank(c.getPara("title"))){
			where.append("and title like ?");
			params.add("'%"+c.getPara("title")+"%'");
		}
		if(StringUtils.isNotBlank(c.getPara("createTimeMin"))){
			where.append("and create_time >= ?");
			params.add(c.getPara("createTimeMin"));
		}
		if(StringUtils.isNotBlank(c.getPara("createTimeMax"))){
			where.append("and create_time < ?");
			params.add(c.getPara("createTimeMax"));
		}
		return dao.paginate(pager.getPn(), pager.getSize(), select, where.toString(), params.toArray());
	}

	public Object getByTitle(String title) {
		String sql = "select * from f_post where title = ?";
		return dao.findFirst(sql, title);
	}

	public void recycle(String... idArr) {
		String sql = "update f_post set state = ? where id = ?";
		Object[][] params = new Object[idArr.length][2];
		for(int i=0;i<idArr.length;i++) {
			params[i][0] = PostStateEnum.DELETE.getCode();
			params[i][1] = idArr[i];
		}
		Db.batch(sql, params, Constants.DEFAULT_BATCH_SIZE);
	}

	public void unRecycle(String[] idArr) {
		String sql = "update f_post set state = ? where id = ?";
		Object[][] params = new Object[idArr.length][2];
		for(int i=0;i<idArr.length;i++) {
			params[i][0] = PostStateEnum.PUBLISH.getCode();
			params[i][1] = idArr[i];
		}
		Db.batch(sql, params, Constants.DEFAULT_BATCH_SIZE);
	}

	public void deleteAll() {
		String sql = "delete from f_post where state = ? ";
		Db.update(sql, PostStateEnum.DELETE.getCode());
	}
	
	/**
	 * get some article by conditions
	 * @param p
	 */
	public void getSome(Map<String, String> p) {
		StringBuffer where = new StringBuffer("select * from f_post where 1=1 and state = '"+PostStateEnum.PUBLISH.getCode()+"'");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(p.get("category"))) {
			where.append("and category = ?");
			params.add(p.get("category"));
		}
		if(StringUtils.isNotBlank(p.get("state"))){
			where.append("and state = ?");
			params.add(p.get("state"));
		}
		if(StringUtils.isNotBlank(p.get("title"))){
			where.append("and title like ?");
			params.add("%"+p.get("title")+"%");
		}
		
		where.append(" order by create_time desc");
	}
	
	/**
	 * get some article by conditions
	 * @param p
	 */
	public void getAllPublic() {
		StringBuffer where = new StringBuffer("select * from f_post where state = '"+PostStateEnum.PUBLISH.getCode()+"' order by create_time desc");
		
		where.append(" ");
	}

}
